//
//  ResetPwd_Vc.swift
//  MikerShop
//
//  Created by eme on 2016/12/6.
//  Copyright © 2016年 eme. All rights reserved.
//

/*
 owner:cy
 update:2016年12月06日10:35:24
 info: 重置密码
 */
import RxSwift
import UtilCore
import RickonCore
import BrandonCore

class ResetPwd_Vc: Base_Vc {
    
    /****************************Storyboard UI设置区域****************************/
    
    @IBOutlet weak var password_Border_V: UIView!
    @IBOutlet weak var password_Tf: UITextField!
    @IBOutlet weak var repeatPassword_Border_V: UIView!
    @IBOutlet weak var repeatPassword_Tf: UITextField!
    @IBOutlet weak var next_Btn: UIButton!
    /*----------------------------   自定义属性区域    ----------------------------*/
    var glshop:GlShopModel?
    var phone:String?
    /****************************Storyboard 绑定方法区域****************************/
    var manageVm :ResetPwd_Vm?
    
    private lazy var secureBtn: UIButton = {
        let button = UIButton()
        button.frame = CGRect(x: 0, y: 0, width: 35, height: 45)
        button.setTitle("", for: .normal)
        button.setImage(UIImage(named: "selected_secure_textfield_btn", in: JonSnowCore.bundle, compatibleWith: nil), for: .selected)
        button.setImage(UIImage(named: "unselected_secure_textfield_btn", in: JonSnowCore.bundle, compatibleWith: nil), for: .normal)
        return button
    }()
    private lazy var repeatSecureBtn: UIButton = {
        let button = UIButton()
        button.frame = CGRect(x: 0, y: 0, width: 35, height: 45)
        button.setTitle("", for: .normal)
        button.setImage(UIImage(named: "selected_secure_textfield_btn", in: JonSnowCore.bundle, compatibleWith: nil), for: .selected)
        button.setImage(UIImage(named: "unselected_secure_textfield_btn", in: JonSnowCore.bundle, compatibleWith: nil), for: .normal)
        return button
    }()
    /**************************** 以下是方法区域 ****************************/
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let identifier = segue.identifier { // 检查是否 nil
            switch identifier {
            case "RegisterDetailInfo_Vc":
                if let vc = segue.destination as? RegisterDetailInfo_Vc {
                    vc.glshop = sender as? GlShopModel
                }
            default: break
            }
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    /**
     界面基础设置
     */
    override func setupUI() {
        /**
         *  自定义 导航栏左侧 返回按钮
         */
        self.customLeftBarButtonItem()
        self.password_Tf.rightView = self.secureBtn
        self.password_Tf.rightViewMode = .whileEditing
        self.secureBtn.isSelected = self.password_Tf.isSecureTextEntry
        self.repeatPassword_Tf.rightView = self.repeatSecureBtn
        self.repeatPassword_Tf.rightViewMode = .whileEditing
        self.repeatSecureBtn.isSelected = self.repeatPassword_Tf.isSecureTextEntry
        self.secureBtn.rx.tap.subscribe{[unowned self] _ in
            self.secureBtn.isSelected = !self.secureBtn.isSelected
            self.password_Tf.isSecureTextEntry = self.secureBtn.isSelected
            }.addDisposableTo(disposeBag)
        self.repeatSecureBtn.rx.tap.subscribe{[unowned self] _ in
            self.repeatSecureBtn.isSelected = !self.repeatSecureBtn.isSelected
            self.repeatPassword_Tf.isSecureTextEntry = self.repeatSecureBtn.isSelected
            }.addDisposableTo(disposeBag)
       
    }
    /**
     app 主题 设置
     */
    override func setViewTheme(){
        self.next_Btn.changeBorderColor(UIColor.clear, cornerRadius: 3.5)
        self.next_Btn.backgroundColor = UIColor.clear
        self.next_Btn.changeBackgroundImage(Theme.btn.normal, highlightedColor: Theme.btn.highlighted, disabledColor: Theme.btn.disabled)
        self.password_Border_V.changeBorderColor(Theme.tf.border, cornerRadius: 2.0,borderWidth:0.5 )
        self.repeatPassword_Border_V.changeBorderColor(Theme.tf.border, cornerRadius: 2.0,borderWidth:0.5 )
        self.password_Tf.changePlaceholder("请设置密码，密码由6-20位字母、数字组合",placeColor:Theme.tf.placeorder,textColor:Theme.tf.text)
        self.repeatPassword_Tf.changePlaceholder("再次输入设置密码",placeColor:Theme.tf.placeorder,textColor:Theme.tf.text)
    }
    /**
     绑定到viewmodel 设置
     */
    override func bindToViewModel(){
        
        self.manageVm = ResetPwd_Vm(input: (
            password: password_Tf.rx.text.orEmpty.asObservable(),
            repeatPassword: repeatPassword_Tf.rx.text.orEmpty.asObservable().map{ repeatPassword in
                return (self.password_Tf.text ?? "" , repeatPassword)},
            nextTaps: next_Btn.rx.tap.asObservable().filter{_ in self.phone != nil}.map{ _ in self.phone!
                
            }
            ), validationService: RegisterImplementations())
        self.manageVm?.signupEnabled
            .subscribe(onNext: {[unowned self] (valid) in
                self.next_Btn.isEnabled = valid
            })
            .addDisposableTo(disposeBag)
        self.manageVm?.error.bindTo(self.view.rx_error).addDisposableTo(disposeBag)
        self.manageVm?.nextSuccess
            .subscribe(onNext: {[unowned self] (rel) in
                self.view.toastCompletion("密码设置成功"){[unowned self] _ in
                    _ = self.navigationController?.popToRootViewController(animated: true)
                }
            })
            .addDisposableTo(disposeBag)
    }
}
